<template>
  <!-- 账户管理 -->
  <div class="account-container">
    <PageCard
      title="查询条件"
      :showTitle="false"
    >
      <el-form
        :model="formData"
        label-position="right"
        :inline="true"
        size="small"
        ref="searchForm"
      >
        <el-form-item prop="name">
          <el-input
            class="w350"
            placeholder="请输入菜单名称"
            v-model="formData.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch()"
          >查询</el-button>
          <el-button @click="handleReset()">重置</el-button>
        </el-form-item>

      </el-form>
    </PageCard>
    <PageCard
      title="菜单列表"
      icon_type="list"
      :hasCreate="true"
      btnText="创建菜单"
      @create="handleAdd('add','parentId')"
    >
      <el-tabs
        v-model="tabKey"
        v-if="userInfo.type != 'admin'"
        @tab-click="handleChangeTab"
        class="tabs-content"
      >
        <el-tab-pane
          v-for="item in tabList"
          :key="item.value"
          :label="item.name"
          :name="item.value"
        >
          <PageTable
            :data="data"
            :currentPage="currentPage"
            :pageSize="pageSize"
            :total="total"
            @handleChange="handleSearch"
          >
            <table-tree-column
              prop="name"
              header-align="center"
              treeKey="id"
              width="260"
              label="菜单名称"
            >
            </table-tree-column>
            <el-table-column
              prop="perms"
              label="菜单编码"
              align="center"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="url"
              label="菜单路由"
              align="center"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="type"
              label="菜单类型"
              align="center"
              header-align="center"
            >
              <template slot-scope="scope">
                <el-tag :type="scope.row.type == 'base' ? 'success':'warning'">
                  {{scope.row.type == 'base' ? '菜单' : '按钮'}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="date"
              label="操作"
              align="right"
              header-align="center"
              width="260"
            >
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.type == 'base'"
                  @click="handleAdd('add','sub',scope.row)"
                  type="text"
                >添加子级菜单</el-button>
                <el-button
                  @click="handleEdit('edit','', scope.row)"
                  type="text"
                >编辑</el-button>
                <el-button
                  class="text-danger"
                  @click="handleDel(scope.row)"
                  type="text"
                >删除</el-button>
              </template>
            </el-table-column>
          </PageTable>
        </el-tab-pane>
      </el-tabs>
      <PageTable
        v-else
        :data="data"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @handleChange="handleSearch"
      >
        <table-tree-column
          prop="name"
          header-align="center"
          treeKey="id"
          width="260"
          label="菜单名称"
        >
        </table-tree-column>
        <el-table-column
          prop="perms"
          label="菜单编码"
          align="center"
          header-align="center"
        ></el-table-column>
        <el-table-column
          prop="url"
          label="菜单路由"
          align="center"
          header-align="center"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="菜单类型"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.type == 'base' ? 'success':'warning'">
              {{scope.row.type == 'base' ? '菜单' : '按钮'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="date"
          label="操作"
          align="right"
          header-align="center"
          width="260"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.type == 'base'"
              @click="handleAdd('add','sub',scope.row)"
              type="text"
            >添加子级菜单</el-button>
            <el-button
              @click="handleEdit('edit','', scope.row)"
              type="text"
            >编辑</el-button>
            <el-button
              class="text-danger"
              @click="handleDel(scope.row)"
              type="text"
            >删除</el-button>
          </template>
        </el-table-column>
      </PageTable>
    </PageCard>
    <Child
      ref="child"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></Child>
  </div>
</template>

<script>
import PageTable from "@/components/PageTable";
import PageCard from "@/components/PageCard";
import TableTreeColumn from "./components/table-tree-column/index";
import child from "./components/child.vue";
import { menusList, deleteMenus } from "@/api/setting1.js";
import { settings } from "nprogress";
import { getUserInfo } from "@/utils/auth";
import * as Utils from "@/utils";
export default {
  name: "Users",
  components: {
    PageTable: PageTable,
    PageCard: PageCard,
    Child: child,
    TableTreeColumn: TableTreeColumn,
  },
  data() {
    return {
      formData: {
        name: "",
        sysFlag: "hospital",
      },
      currentPage: 1,
      pageSize: 40,
      total: 0,
      data: [],
      tabKey: "hospital",
      tabList: [
        {
          name: "医院端",
          value: "hospital",
        },
        {
          name: "APP端",
          value: "staff",
        },
      ], //
      userInfo: {},
    };
  },
  created() {
    this.userInfo = getUserInfo() ? JSON.parse(getUserInfo()) : {};
    this.handleSearch(1, 40);
  },
  methods: {
    //切换菜单列表
    handleChangeTab(row) {
      this.tabKey = row.name;
      this.formData.sysFlag = row.name;
      this.handleSearch(1, 40);
    },
    // 查询
    handleSearch(current, limit) {
      this.currentPage = current;
      this.pageSize = limit;
      const params = {
        ...this.formData,
        pageIndex: current || 1,
        pageSize: limit || 40,
      };
      // 调接口查询
      this.handleGetDataList(params);
    },
    //获取列表数据
    handleGetDataList() {
      let params = {
        isPage: false,
        sysFlag:
          this.userInfo.type == "admin" ? "admin" : this.formData.sysFlag,
      };
      menusList(params).then((res) => {
        if (res && res.success) {
          this.data = res.data ? Utils.treeDataTranslate(res.data) : [];
          this.total = 0;
        }
      });
    },
    // 重置
    handleReset() {
      this.$refs["searchForm"].resetFields();
    },
    handleAdd(type, level, row) {
      this.$refs.child.showModal(type, level, row);
    },
    handleEdit(type, level, row) {
      this.$refs.child.showModal(type, level, row);
    },

    //删除用户
    handleDel(item) {
      this.$confirm("此操作将永久删除该菜单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteMenus(item.id).then((res) => {
            if (res && res.success) {
              this.handleSearch(this.currentPage, this.pageSize);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分配角色
    handleRole(row) {
      this.$refs.chooseRole.showModal("edit", row);
    },
    //重置密码
    handleResetPwd() {},
  },
};
</script>

<style lang="scss" scoped>
.account-container {
  .btns {
    text-align: right;
  }
}
</style>
